#!/usr/bin/env python

import argparse
import sys
import os
sys.path.append("/Users/roosevelt/Desktop/bayes/trunk/modules/")
#sys.path.append("/home/roosevelt/Desktop/bayes/trunk/modules/")
from BayesNetwork import BayesNetwork

# Building arguments help
parser = argparse.ArgumentParser(description='Generate a bayesian network experiment.')
parser.add_argument('-r', help='The reference name of the experiment')
parser.add_argument('-f', help='The folder where the experiment will be created')
parser.add_argument('-n', help='The number of folds for the experiment')
parser.add_argument('-d', help='The path to the dataset')
parser.add_argument('-m', help='The experiment model to be used')
parser.add_argument('-s', help='The initial structure to be used in the experiment')
parser.add_argument('-t', help='The target structure to be used in the experiment')
parser.add_argument('-c', help='Class variable to be analyzed.')

#Parsing passed arguments
args = parser.parse_args()

# Getting variables
ref =       vars(args)['r']
folder =    vars(args)['f']
nfolds =    vars(args)['n']
dataset =   vars(args)['d']
model =     vars(args)['m']
structure = vars(args)['s']
target =    vars(args)['t']
class_var = vars(args)['c']


BN = BayesNetwork()

if model and folder and ref and structure and dataset:
    # Creating folders
    os.system("mkdir " + folder + "/" + ref)
    for i in range(int(nfolds)):
        os.system("mkdir " + folder + "/" + ref + "/" + str(i))
                
    folder_ref = folder  + ref + "/"
    # Copying files
    # Copy dataset
    os.system("cp " + dataset + " " + folder_ref)
    # Copy initial structure
    os.system("cp " + structure + " " + folder_ref)
    # Copy target structure
    os.system("cp " + target + " " + folder_ref)
    
    # Building model
    # Copy models
    os.system("cp " + model + "/exec.py " + folder_ref + "exec_tmp.py")
    os.system("cp " + model + "/experiments.py " + folder_ref + "experiments_tmp.py")
    # Create files to receive custom models
    os.system("touch " + folder_ref + "/exec.py")
    os.system("touch " + folder_ref + "/experiments.py")
    
    # Make changes to the models
    structure_filename = structure.split("/")[-1]
    dataset_filename = dataset.split("/")[-1]
    target_filename = target.split("/")[-1]
    
    path_structure = ("../../../data/ini_structures/" + structure_filename).replace("/", "\/")
    path_dataset = ("../../../data/dataset/" + dataset_filename).replace("/", "\/")
    path_target = ("../../../data/EncogBayesData/" + target_filename).replace("/", "\/")
    
    
    os.system("sed 's/\$STRUCTURE/" + path_structure + "/g; s/\$DATASET/" + path_dataset + "/g; s/\$TARGET/" + path_target + "/g; s/\$CLASSVAR/" + class_var + "/g' "+ folder_ref + "exec_tmp.py >> " + folder_ref + "/exec.py")
    os.system("sed 's/\$STRUCTURE/" + path_structure + "/g; s/\$DATASET/" + path_dataset + "/g; s/\$TARGET/" + path_target + "/g; s/\$CLASSVAR/" + class_var + "/g' "+ folder_ref + "experiments_tmp.py >> " + folder_ref + "/experiments.py")    
    
    os.system("rm " + folder_ref + "exec_tmp.py")
    os.system("rm " + folder_ref + "experiments_tmp.py")
    
    alg = folder.split("/")[-2]
    
    print "oarsub -d $HOME/experiment -n "+ ref +"_job -O $HOME/experiment/output/%jobid%.out -E $HOME/experiment/error/%jobid%.error -l /core=4,walltime=72:00:00 \"$HOME/experiment/exec.sh '" + alg + "' '" + ref + "' \" -p \"cluster='barra'\"" 

    
    

